# -*- coding: UTF-8 -*_
from PIL import Image
from pytesseract import *
import requests
from io import BytesIO
def yzm(s):        #s是图片本地路径
    im = Image.open(s)
    #im.show()
    imgry = im.convert('L')#把图片变成灰色
    threshold = 140

    table = []

    for i in range(256):#二值化变成黑白

        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    out = imgry.point(table, '1')
    asd = pytesseract.image_to_string(out,lang='fyx')#然后就是机器识别咯，lang包来自小贩子的喂图调教成果（机器学习）~~~
    return  asd


zh=input('输入账号:\n')
mm=input('输入密码:\n')
img_src='http://202.119.113.135/validateCodeAction.do?random=0.5'#获取验证码的链接
url='http://202.119.113.135/gradeLnAllAction.do?type=ln&oper=qbinfo&lnxndm'
pm_url='http://202.119.113.135/yxsapplyAction.do?oper=zzy'
url1='http://202.119.113.135/loginAction.do'#登录链接
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/52.0'}#请求头
'''
fw函数是用来验证密码是否正确的函数
mm参数是密码
xc参数是线程编号~~
'''
def gs(s):
    s=s.replace(' ','')
    s=s.replace('\t','')
    s=s.replace('\n','')
    return s
def dl(zh,mm):#登录
    valcode = requests.get(img_src) #从验证码链接获取图片
    cookies = valcode.cookies.get_dict() #保存cookies
    image = Image.open(BytesIO(valcode.content)) #图片获取
    #path1 = os.path.abspath('.')
    lj='yzm.bmp'#图片保存路径
    try:
        image.save(lj)#保存图片
    except:
        return dl(zh,mm)
    yzm_str = yzm(lj)
    if (len(yzm_str) != 4):#验证码不是4位说明识别出错
        return dl(zh,mm)
    mms = str(mm)
    while (len(mms) < 6):#密码补0
        mms = '0' + mms
    data = {        #需要post的数据
        "zjh": str(zh),
        "mm": mms,
        "v_yzm": yzm_str,
    }
    response = requests.post(url1, data=data, headers=headers, cookies=cookies)#获取网页
    htxt = response.text#网页源代码
    datas={
        'oper':'zzy',
    }
    if ('您的密码不正确，请您重新输入' in htxt):

        print('密码错误')
        return '0'
    elif ('你输入的验证码错误' in htxt):
        return dl(zh, mm)
    else:
        response = requests.post(pm_url,data=datas, headers=headers, cookies=cookies)  # 获取网页
        return response.text

s1=dl(zh,mm)
if(s1=='0'):
    print('导出失败')
else:
    pass
s1=s1.split('var jdcj = \'')[1]
jd=s1.split('\'')[0]
s1=s1.split('绩点排名')[1]
jdpm=s1.split('<td width="20%" >')[1]
jdpm=jdpm.split('</td>')[0]
s1=s1.split('平均成绩')[1]
pjcj=s1.split('<td width="35%" >')[1]
pjcj=pjcj.split('</td>')[0]
pjcj=gs(pjcj)
jdpm=gs(jdpm)
print(jd)
print(jdpm)
print(pjcj)
